package spotor.client.service;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;

public class SpotorLocation {
	
    /**
     * @param context
     * @return
     */
	//Get the Location by GPS or WIFI  
    public Location getLocation(Context context) {  
        LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);  
        Location location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
        if (location == null) {  
            location = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
        }  
        return location;  
    }  
    //  
     public GeoPoint getGeoByLocation(Location location) {  
         GeoPoint gp = null;  
         try {  
             if (location != null) {  
                 double geoLatitude = location.getLatitude() * 1E6;  
                 double geoLongitude = location.getLongitude() * 1E6;  
                 gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);  
             }  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
         return gp;  
     }  
     //GeoPointAddress  
     public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {  
         Address result = null;  
         try {  
             if (gp != null) {  
                 Geocoder gc = new Geocoder(cntext, Locale.CHINA);  
                  
                 double geoLatitude = (int) gp.getLatitudeE6() / 1E6;  
                 double geoLongitude = (int) gp.getLongitudeE6() / 1E6;  
                   
                 List<Address> lstAddress = gc.getFromLocation(geoLatitude,  
                         geoLongitude, 1);  
                 if (lstAddress.size() > 0) {  
                     result = lstAddress.get(0);  
                 }  
             }  
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
         return result;  
     }  
     public double getDistance(double lat1, double lon1, double lat2, double lon2) {  
    	    float[] results=new float[1];  
    	    Location.distanceBetween(lat1, lon1, lat2, lon2, results);  
    	    return results[0];  
    	} 
}
